package com.example.libowu.musicplayer;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by libowu on 17-12-9.
 */

public class MusicAdapter extends RecyclerView.Adapter<MusicAdapter.Adapter>{
    private List<Music> musicList;
    private onMusicList on;


    public class Adapter extends RecyclerView.ViewHolder {
        private TextView music_name;
        private TextView author_name;
        private LinearLayout music_item;
        public Adapter(View itemView) {
            super(itemView);
            music_name=(TextView) itemView.findViewById(R.id.song_name);
            author_name=(TextView) itemView.findViewById(R.id.author_name);
            music_item=(LinearLayout) itemView.findViewById(R.id.music_item);
        }
    }

    public MusicAdapter(List<Music> musicList,onMusicList on){
        this.musicList=musicList;
        this.on=on;
    }
    @Override
    public Adapter onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.music_list,parent,false);
        Adapter a=new Adapter(view);
        return a;
    }

    @Override
    public void onBindViewHolder(Adapter holder, final int position) {
        final Music gm=musicList.get(position);
        holder.author_name.setText(gm.getAuthorN());
        holder.music_name.setText(gm.getSongN());

        holder.music_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try{
                    on.onMusicList(gm.getSongPath(),gm.getSongImgPath(),gm.getSongN(),position);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return musicList.size();
    }

}
